package com.xxrl.hmwork.controller;

import com.xxrl.hmwork.commons.Jackson;
import com.xxrl.hmwork.commons.MailUtils;
import com.xxrl.hmwork.commons.StringUtils;
import com.xxrl.hmwork.domain.User;
import com.xxrl.hmwork.service.StatisticsService;
import com.xxrl.hmwork.service.UserService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 用户逻辑
 * @author mis
 */
@RestController
@RequestMapping("/user")
public class UserController {

    private final StatisticsService statisticsService;
    private final UserService userService;
    private final MailUtils mailUtils;

    @Autowired
    public UserController(StatisticsService statisticsService, UserService userService, MailUtils mailUtils) {
        this.statisticsService = statisticsService;
        this.userService = userService;
        this.mailUtils = mailUtils;
    }

    @PostMapping("login")
    public ResponseEntity<String> login(String username, String password, HttpSession session) {
        User user = userService.login(username,password);
        if (null == user) {
            return ResponseEntity.badRequest().body("UserNotFound");
        }
        session.setAttribute("token", user.getType());
        session.setAttribute("user", user);

        statisticsService.visit(user.getType());

        return ResponseEntity.accepted().body(userService.getToken(user.getType()));
    }

    @GetMapping("info")
    public ResponseEntity info(@NotNull HttpSession session) {

        User user = (User) session.getAttribute("user");
        if (null == user) {
            return ResponseEntity.badRequest().body("Login failed, unable to get user details.");
        }
        return ResponseEntity.accepted().body(userService.getInfo(user.getType(),user.getName()));
    }

    @PostMapping("logout")
    public String logout(@NotNull HttpSession session) {
        session.removeAttribute("token");
        session.removeAttribute("user");
        return "success";
    }

    @GetMapping("find")
    public ResponseEntity<String> find(String name) {
        List<User> users = userService.find(name);
        if (null == users || users.size() == 0) {
            return ResponseEntity.badRequest().body("UserNotFound");
        }
        return ResponseEntity.accepted().body(Jackson.me().toJson(users));
    }

    @PostMapping("registration")
    public ResponseEntity<String> registration(@NotNull User user) {
        user.setType(1);
        boolean status = userService.registration(user);
        if (!status) {
            return ResponseEntity.badRequest().body("UserExist");
        }
        return ResponseEntity.accepted().body("success");
    }

    @PostMapping("backPasswd")
    public ResponseEntity<String> backPassword(@RequestParam(name = "username") String name
            , @RequestParam(name = "email") String email
            , @RequestParam(name = "telphone") String phone) {
        String password = userService.lookPassword(name, email, phone);
        if (StringUtils.isEmpty(password)) {
            return ResponseEntity.badRequest().body("UserNotFound");
        }
        //为用户发找回密码邮件
        mailUtils.sendMail(2,password,email);
        return ResponseEntity.accepted().body(password);
    }

    @GetMapping("list")
    public String list(
            @RequestParam(name = "page", defaultValue = "0") Integer page,
            @RequestParam(name = "limit", defaultValue = "20") Integer limit) {
        page = page == 0 ? 0 : page-1;
        Page<User> list = userService.list(PageRequest.of(page, limit));
        String json = Jackson.me().toJson(list.toList());
        statisticsService.totalUser = list.getTotalElements();
        json = "{ \"items\":" +json + ", \"total\":" + list.getTotalElements()+"}";
        return json;
    }
}
